//
//  TopicDetailTableViewCell.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/28.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "TopicDetailTableViewCell.h"
#import "baseImageView.h"
#import "baseLabel.h"
#import "BaseView.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>

@interface TopicDetailTableViewCell ()
@property (nonatomic, retain) baseImageView *imgUserPhoto;  /**< 用户头像 */
@property (nonatomic, retain) baseLabel *labelUserName; /**< 用户名 */
@property (nonatomic, retain) baseLabel *labelTime; /**< 时间 */

@property (nonatomic, retain) BaseView *viewBackGround; /**< 白色背景 */

@property (nonatomic, retain) baseLabel *labelTitle; /**< 题目 */
@property (nonatomic, retain)baseImageView *imgPhoto;  /**< 图片 */


@property (nonatomic, retain) baseImageView *imgComment;  /**< 评论图标 */
@property (nonatomic, retain) baseLabel *labelCommentCount; /**< 评论数 */

@property (nonatomic, retain) baseImageView *imgZan; /**< 赞图标 */

@property (nonatomic, retain) baseLabel *labelZan; /**< 赞 */

@property (nonatomic, retain) baseImageView *imgAlert; /**< 弹出图标 */

@property (nonatomic, retain) BaseView *viewLine; /**< 线 */
@property (nonatomic, retain) baseLabel *labelComment; /**< 用户名 + 评论 */
@property (nonatomic, retain) baseLabel *labelSeeAll; /**< 查看所有回应 */
@end

@implementation TopicDetailTableViewCell


#pragma mark - ------------ 内存管理 ---------------

- (void)dealloc
{
    [_imgUserPhoto release];
    [_labelUserName release];
    [_labelTime release];
    [_labelTitle release];
    [_imgPhoto release];
    [_imgComment release];
    [_labelCommentCount release];
    [_imgZan release];
    [_labelZan release];
    [_imgAlert release];
    [_viewBackGround release];
    [_viewLine release];
    [_labelComment release];
    [_labelSeeAll release];
    [_model release];
    [super dealloc];
}

#pragma mark - ------------ 初始化 ---------------
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
//        self.contentView.dk_backgroundColorPicker = DKColorWithColors(GrayLight,[UIColor lightGrayColor]);
        [self creatSubViews];
    }
    return self;
    
    
}

#pragma mark - ------------ 创建子视图 ---------------
- (void)creatSubViews
{
    self.imgUserPhoto = [[baseImageView alloc]init];
    [self.contentView addSubview:_imgUserPhoto];
    _imgUserPhoto.layer.cornerRadius = 20;
    _imgUserPhoto.clipsToBounds = YES;
    
    
    self.labelUserName = [[baseLabel alloc]init];
    [self.contentView addSubview:_labelUserName];
    _labelUserName.textColor = [UIColor colorWithRed:0.04 green:0.64 blue:0.85 alpha:1.00];
    _labelUserName.font = [UIFont systemFontOfSize:13 weight:15];
    
    
    self.labelTime = [[baseLabel alloc]init];
    [self.contentView addSubview:_labelTime];
    _labelTime.font = [UIFont systemFontOfSize:15];
    /** 夜间 */
//    _labelTime.dk_textColorPicker = DKColorWithColors([UIColor lightGrayColor],[UIColor grayColor]);
    
    self.viewBackGround = [[BaseView alloc]init];
    /** 夜间 */
//    _viewBackGround.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor grayColor]);
    [self.contentView addSubview:_viewBackGround];
    
    
    self.labelTitle = [[baseLabel alloc]init];
    [self.viewBackGround addSubview:_labelTitle];
    _labelTitle.numberOfLines = 0;
    _labelTitle.font =[UIFont systemFontOfSize:15];
    /** 夜间 */
//    _labelTitle.dk_textColorPicker = DKColorWithColors([UIColor grayColor],[UIColor lightGrayColor]);
    
    
    
    self.imgPhoto = [[baseImageView alloc]init];
    [self.viewBackGround addSubview:_imgPhoto];
    _imgPhoto.contentMode = UIViewContentModeScaleAspectFill;
    _imgPhoto.clipsToBounds = YES;
    
    
    
    self.imgComment = [[baseImageView alloc]init];
    [self.viewBackGround addSubview:_imgComment];
    
    
    
    self.labelCommentCount = [[baseLabel alloc]init];
    [self.viewBackGround addSubview:_labelCommentCount];
    _labelCommentCount.textColor = [UIColor lightGrayColor];
    _labelCommentCount.font = [UIFont systemFontOfSize:13];
    
    
    
    self.imgZan = [[baseImageView alloc]init];
    [self.viewBackGround addSubview:_imgZan];
    
    self.labelZan = [[baseLabel alloc]init];
    [self.viewBackGround addSubview:_labelZan];
    _labelZan.textColor = [UIColor lightGrayColor];
    _labelZan.font = [UIFont systemFontOfSize:13];
    
    
    self.imgAlert = [[baseImageView alloc]init];
    [self.viewBackGround addSubview:_imgAlert];
    
    
    self.viewLine = [[BaseView alloc]init];
    [self.viewBackGround addSubview:_viewLine];
    
    
    self.labelComment = [[baseLabel alloc]init];
    [self.viewBackGround addSubview:_labelComment];
    _labelComment.numberOfLines = 0;
    _labelComment.font = [UIFont systemFontOfSize:15];
    /** 夜间 */
//    _labelComment.dk_textColorPicker = DKColorWithColors([UIColor grayColor],[UIColor lightGrayColor]);
    
    
    
    self.labelSeeAll = [[baseLabel alloc]init];
    [self.viewBackGround addSubview:_labelSeeAll];
    _labelSeeAll.textColor = [UIColor lightGrayColor];
    _labelSeeAll.font = [UIFont systemFontOfSize:13];
    
    _viewLine.backgroundColor = [UIColor lightGrayColor];
    
    
    [_imgUserPhoto release];
    [_labelUserName release];
    [_labelTime release];
    [_labelTitle release];
    [_imgPhoto release];
    [_imgComment release];
    [_labelCommentCount release];
    [_imgZan release];
    [_labelZan release];
    [_imgAlert release];
    [_viewBackGround release];
    [_viewLine release];
    [_labelComment release];
    [_labelSeeAll release];
    
    
//    
//    _imgUserPhoto.backgroundColor = [UIColor yellowColor];
//    _labelUserName.backgroundColor = [UIColor redColor];
//    _labelTime.backgroundColor = [UIColor orangeColor];
//    
//    _labelTitle.backgroundColor = [UIColor yellowColor];
//    _imgPhoto.backgroundColor = [UIColor redColor];
//    
//    _imgZan.backgroundColor = [UIColor redColor];
//    _labelZan.backgroundColor = [UIColor grayColor];
//    _imgComment.backgroundColor = [UIColor redColor];
//    _labelCommentCount.backgroundColor =[ UIColor grayColor];
//    
//    _imgAlert.backgroundColor = [UIColor redColor];
//    
//    _labelComment.backgroundColor = [UIColor yellowColor];
//    _labelSeeAll.backgroundColor = [UIColor redColor];
    
    
}


#pragma mark - ------------ 设置大小 ---------------
- (void)layoutSubviews
{
    [super layoutSubviews];

    
    [_imgUserPhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.top.mas_equalTo(self.contentView).offset(10);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    
    [_labelUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_imgUserPhoto);
        make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(10);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(30);
    }];
    
    
    [_labelTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imgUserPhoto);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.height.mas_equalTo(_labelUserName);
    }];
    
    CGFloat h = [[self class]heightWithText:_model.content];
    CGFloat h1 = [[self class]heightWithText:_labelComment.text];
    
    /**< 既有图片又有评论 */
    if (_model.path != nil && _model.replayContent != nil) {
        
        
        
        [_viewBackGround mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelUserName.mas_bottom).offset(5);
            make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(5);
            make.right.mas_equalTo(self.contentView).offset(-10);
            make.height.mas_equalTo(h + h1 + 271);
        }];
        
        
        [_labelTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(h);
        }];
        
        _imgPhoto.hidden = NO;
        
        [_imgPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(10);
            make.left.mas_equalTo(_viewBackGround).offset(10);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(120);
            
        }];
        
        
        [_imgComment mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(_viewBackGround).offset(15);
            make.top.mas_equalTo(_imgPhoto.mas_bottom).offset(20);
            make.height.width.mas_equalTo(25);
            
        }];
        
        [_labelCommentCount mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgComment.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(30);
        }];
        
        [_imgZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_labelCommentCount.mas_right).offset(60);
            make.width.height.mas_equalTo(_imgComment);
            
        }];
        
        [_labelZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgZan.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(_labelCommentCount);
            
        }];
        
        [_imgAlert mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.width.height.mas_equalTo(_imgComment);
            
        }];
        
        _viewLine.hidden = NO;
        [_viewLine mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.right.mas_equalTo(_viewBackGround).offset(0);
            make.top.mas_equalTo(_imgComment.mas_bottom).offset(10);
            make.height.mas_equalTo(1);
            
        }];
        
        _labelComment.hidden = NO;
        [_labelComment mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_viewLine.mas_bottom).offset(20);
            make.left.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(h1);
            
        }];
        
        _labelSeeAll.hidden = NO;
        [_labelSeeAll mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelComment.mas_bottom).offset(10);
            make.left.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(30);
            
        }];
        
        
        /** 图片为空 评论为空 */
    }
    else if(_model.path == nil  &&  _model.replayContent == nil ){
        
        [_viewBackGround mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelUserName.mas_bottom).offset(5);
            make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(5);
            make.right.mas_equalTo(self.contentView).offset(-10);
            make.height.mas_equalTo(h + 60);
        }];
        
        
        [_labelTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(h);
        }];
        
        
        _imgPhoto.hidden = YES;
        
        
        [_imgComment mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(_viewBackGround).offset(15);
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(20);
            make.height.width.mas_equalTo(25);
        }];
        
        [_labelCommentCount mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgComment.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(30);
        }];
        
        [_imgZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_labelCommentCount.mas_right).offset(60);
            make.width.height.mas_equalTo(_imgComment);
        }];
        
        [_labelZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgZan.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(_labelCommentCount);
        }];
        
        [_imgAlert mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.width.height.mas_equalTo(_imgComment);
        }];
        
        _viewLine.hidden = YES;
        
        _labelComment.hidden = YES;
        
        _labelSeeAll.hidden = YES;
        
        
        /** 图片为空 有评论 */
    }
    else if (_model.path == nil && _model.replayContent != nil){
        
        [_viewBackGround mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelUserName.mas_bottom).offset(5);
            make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(5);
            make.right.mas_equalTo(self.contentView).offset(-10);
            make.height.mas_equalTo(h + h1 + 141);
        }];
        
        [_labelTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(h);
        }];
        
        
        _imgPhoto.hidden = YES;
        
        
        [_imgComment mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(_viewBackGround).offset(15);
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(20);
            make.height.width.mas_equalTo(25);
            
        }];
        
        [_labelCommentCount mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgComment.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(30);
        }];
        
        [_imgZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_labelCommentCount.mas_right).offset(60);
            make.width.height.mas_equalTo(_imgComment);
        }];
        
        [_labelZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgZan.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(_labelCommentCount);
        }];
        
        [_imgAlert mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.width.height.mas_equalTo(_imgComment);
            
        }];
        
        _viewLine.hidden = NO;
        [_viewLine mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.right.mas_equalTo(_viewBackGround).offset(0);
            make.top.mas_equalTo(_imgComment.mas_bottom).offset(10);
            make.height.mas_equalTo(1);
            
        }];
        
        _labelComment.hidden = NO;
        [_labelComment mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_viewLine.mas_bottom).offset(20);
            make.left.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(h1);
            
        }];
        
        _labelSeeAll.hidden = NO;
        [_labelSeeAll mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelComment.mas_bottom).offset(10);
            make.left.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(30);
            
        }];
        /** 图片有 无评论 */
    }
    else if (_model.path != nil && _model.replayContent == nil){
        
        
        [_viewBackGround mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelUserName.mas_bottom).offset(5);
            make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(5);
            make.right.mas_equalTo(self.contentView).offset(-10);
            make.height.mas_equalTo(h + 190);
        }];
        
        
        [_labelTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.mas_equalTo(_viewBackGround).offset(10);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.height.mas_equalTo(h);
        }];
        
        _imgPhoto.hidden = NO;
        [_imgPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(10);
            make.left.mas_equalTo(_viewBackGround).offset(10);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(120);
            
        }];
        
        [_imgComment mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(_viewBackGround).offset(15);
            make.top.mas_equalTo(_imgPhoto.mas_bottom).offset(20);
            make.height.width.mas_equalTo(25);
            
        }];
        
        [_labelCommentCount mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgComment.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(30);
        }];
        
        [_imgZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_labelCommentCount.mas_right).offset(60);
            make.width.height.mas_equalTo(_imgComment);
        }];
        
        [_labelZan mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.left.mas_equalTo(_imgZan.mas_right).offset(5);
            make.height.mas_equalTo(_imgComment);
            make.width.mas_equalTo(_labelCommentCount);
        }];
        
        [_imgAlert mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_imgComment);
            make.right.mas_equalTo(_viewBackGround).offset(-10);
            make.width.height.mas_equalTo(_imgComment);
            
        }];
        
        _viewLine.hidden = YES;
        
        _labelComment.hidden = YES;
        
        _labelSeeAll.hidden = YES;
        
    }
}

- (void)setModel:(TopicModel *)model
{
    
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    [_imgUserPhoto sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    _labelUserName.text = model.username;
    
    /** 时间戳转换 */
    _labelTime.text = [[self class]howlongToCurrntTime:model.add_datetime_ts.stringValue];
    _labelTitle.text = model.content;
    
    [_imgPhoto sd_setImageWithURL:[NSURL URLWithString:model.path]];
    
    _imgComment.image = [UIImage imageNamed:@"ping"];
    
    _labelCommentCount.text = model.reply_count.stringValue;
    
    _imgZan.image = [UIImage imageNamed:@"zan"];
    _labelZan.text = model.like_count.stringValue;
    
    _imgAlert.image = [UIImage imageNamed:@"gengduo"];
    
    _labelComment.text = [[model.replayUsername stringByAppendingString:@":   "] stringByAppendingString:model.replayContent];
    
    _labelSeeAll.text = @"查看所有回应";
}

#pragma mark - ------------ 文本高度自适应 ---------------
+ (CGFloat)heightWithText:(NSString *)text
{
    
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 80, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
    
    return rect.size.height;
    
}

#pragma mark - ------------ 时间转换 ---------------
+ (NSString *)howlongToCurrntTime:(NSString *)time
{
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time.doubleValue];
    
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc]init];
    [dateFomatter setDateFormat:@"M月dd日 HH:mm"];
    
    //1970  转至今的时间
    NSString *fomatterString = [dateFomatter stringFromDate:date];
    
    [dateFomatter release];
    
    return fomatterString;
    
    
}
























@end
